package tech.runchen.gz5055.oms.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Query;
import tech.runchen.gz5055.oms.common.utils.SecurityUtils;
import tech.runchen.gz5055.oms.common.utils.StringUtils;
import tech.runchen.gz5055.oms.modules.system.entity.FingerprintEntity;
import tech.runchen.gz5055.oms.modules.system.entity.UserEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.UserMapper;
import tech.runchen.gz5055.oms.modules.system.service.IFingerprintService;
import tech.runchen.gz5055.oms.modules.system.service.IUserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-02-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Autowired
    private IFingerprintService fingerprintService;

    @Override
    public PageInfo queryPage(Map<String, Object> params) {

        String username = (String) params.get("username");
        String nikeName = (String) params.get("nikename");

        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
                        .like(StringUtils.isNotBlank(username), "user_name", username)
                        .like(StringUtils.isNotBlank(nikeName), "nick_name", nikeName)
        );

        return new PageInfo(page);
    }

    @Override
    public UserEntity queryByUserName(String username) {
        return baseMapper.queryByUserName(username);
    }

    @Override
    public boolean saveUser(UserEntity user) {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return this.save(user);
    }

    @Override
    public void updateUser(UserEntity user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        }
        this.updateById(user);
    }

    @Override
    public boolean deleteBatch(Long[] userIds) {
        return this.removeByIds(Arrays.asList(userIds));
    }

    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        UserEntity userEntity = new UserEntity();
        userEntity.setPassword(newPassword);
        return this.update(userEntity,
                new QueryWrapper<UserEntity>().eq("id", userId).eq("password", password));

    }

    @Override
    public boolean updatePsChar(Long userId, String psChar) {
        FingerprintEntity fingerprint = new FingerprintEntity();
        fingerprint.setPsChar(psChar);
        return fingerprintService.update(fingerprint,
                new QueryWrapper<FingerprintEntity>().eq("id", userId));
    }

    @Override
    public List<Long> queryUserIdList(Integer type) {
        return baseMapper.queryUserIdList(type);
    }
}
